home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / util / mouse / FreeWheel.lha / FreeWheel / Cx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-26  |  3.2 KB  |  163 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <exec/types.h>
  6. #include <exec/ports.h>
  7. #include <exec/memory.h>
  8. #include <libraries/commodities.h>
  9. #include <clib/exec_protos.h>
  10. #include <clib/commodities_protos.h>
  11. #include <clib/alib_protos.h>
  12.  
  13. #include "Icon.h"
  14.  
  15. #include "Cx.h"
  16.  
  17. BOOL HandleCxMessages(struct CxContext *cx,unsigned long signals);
  18. void DisposeCxContext(struct CxContext *cx);
  19. BOOL Cx_SetHotKey(struct CxContext *cx,char *hotkey);
  20. BOOL Cx_SetCustomRoutine(struct CxContext *cx,void (*rout)(CxMsg *msg,CxObj *obj));
  21.  
  22.  
  23. BOOL Cx_SetHotKey(struct CxContext *cx,char *hotkey)
  24. {
  25.   if(cx->HotKey)
  26.     DeleteCxObjAll(cx->HotKey);
  27.   if(cx->HotKey=HotKey(hotkey,cx->Port,CXCMD_APPEAR))
  28.     AttachCxObj(cx->Broker,cx->HotKey);
  29.   if(cx->HotKey)
  30.     return(TRUE);
  31.   else
  32.     return(FALSE);
  33. }
  34.  
  35.  
  36. BOOL Cx_SetCustomRoutine(struct CxContext *cx,void (*rout)(CxMsg *msg,CxObj *obj))
  37. {
  38.   if(cx->CustomObject)
  39.     DeleteCxObjAll(cx->CustomObject);
  40.   if(cx->CustomObject=CxCustom(rout,0))
  41.     AttachCxObj(cx->Broker,cx->CustomObject);
  42.   if(cx->CustomObject)
  43.     return(TRUE);
  44.   else
  45.     return(FALSE);
  46. }
  47.  
  48.  
  49. BOOL HandleCxMessages(struct CxContext *cx,unsigned long signals)
  50. {
  51.   struct Message *msg;
  52.   long id;
  53.   BOOL result=TRUE;
  54.   if(cx)
  55.   {
  56.     if(signals&cx->Signals)
  57.     {
  58.       while(msg=GetMsg(cx->Port))
  59.       {
  60.         id=CxMsgID((CxMsg *)msg);
  61.         ReplyMsg(msg);
  62.         switch(id)
  63.         {
  64.           case CXCMD_DISABLE:
  65.             ActivateCxObj(cx->Broker,FALSE);
  66.             ActivateCxObj(cx->CustomObject,FALSE);
  67.             break;
  68.           case CXCMD_ENABLE:
  69.             ActivateCxObj(cx->Broker,TRUE);
  70.             ActivateCxObj(cx->CustomObject,TRUE);
  71.             break;
  72.           case CXCMD_UNIQUE:
  73.           case CXCMD_APPEAR:
  74.             if(cx->ShowCallback)
  75.               cx->ShowCallback(cx);
  76.             break;
  77.           case CXCMD_DISAPPEAR:
  78.             if(cx->HideCallback)
  79.               cx->HideCallback(cx);
  80.             break;
  81.           case CXCMD_KILL:
  82.             result=FALSE;
  83.             break;
  84.         }
  85.       }
  86.     }
  87.   }
  88.   return(result);
  89. }
  90.  
  91.  
  92. struct CxContext *CxContext_Create(char *name,char *title,char *descr,void *userdata)
  93. {
  94.   struct CxContext *cx;
  95.  
  96.   struct NewBroker MyNewBroker =
  97.   {
  98.     NB_VERSION,
  99.     NULL,
  100.     NULL,
  101.     NULL,
  102.     NBU_UNIQUE|NBU_NOTIFY,
  103.     COF_SHOW_HIDE,
  104.     127,
  105.     NULL
  106.   };
  107.  
  108.   MyNewBroker.nb_Name=name;
  109.   MyNewBroker.nb_Title=title;
  110.   MyNewBroker.nb_Descr=descr;
  111.  
  112.   if(!(cx=malloc(sizeof(struct CxContext))))
  113.     return(NULL);
  114.   memset(cx,0,sizeof(struct CxContext));
  115.  
  116.   cx->Dispose=DisposeCxContext;
  117.   cx->UserData=userdata;
  118.   cx->Handle=HandleCxMessages;
  119.   cx->SetHotKey=Cx_SetHotKey;
  120.   cx->SetCustom=Cx_SetCustomRoutine;
  121.  
  122.   cx->HotKey=NULL;
  123.   cx->CustomObject=NULL;
  124.  
  125.   if(!(cx->Port=CreateMsgPort()))
  126.   {
  127.     cx->Dispose(cx);
  128.     return(NULL);
  129.   }
  130.   cx->Signals=(1<<cx->Port->mp_SigBit);
  131.   MyNewBroker.nb_Port=cx->Port;
  132.  
  133.   if(!(cx->Broker=CxBroker(&MyNewBroker,NULL)))
  134.   {
  135.     cx->Dispose(cx);
  136.     return(NULL);
  137.   }
  138.  
  139.   ActivateCxObj(cx->Broker,TRUE);
  140.  
  141.   return(cx);
  142. }
  143.  
  144.  
  145. void DisposeCxContext(struct CxContext *cx)
  146. {
  147.   if(cx)
  148.   {
  149.     if(cx->Broker)
  150.     {
  151.       DeleteCxObjAll(cx->Broker);
  152.       cx->Broker=NULL;
  153.     }
  154.     if(cx->Port)
  155.     {
  156.       DeleteMsgPort(cx->Port);
  157.       cx->Port=NULL;
  158.     }
  159.     free(cx);
  160.   }
  161. }
  162.  
  163.